package search;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class WebSpiderImpl implements WebSpider {
    public Parser getParser() {
        Parser parser = new ParserImpl();
        return parser;
    }

    public List<String> getHtmlFromWeb() {
        HttpURLConnection conn = null;
        URL url = null;
        InputStream in = null;
        BufferedReader reader = null;
        //StringBuffer stringBuffer = null;
        List<String> list= new ArrayList<String>();

        try {
            Document doc = Jsoup.connect("http;//prog-crs.ust.hk/pgprog").get();
            Elements schools = doc.select("div.calaog-program-wrapper");
            Elements courses = schools.select("div.school");
            Elements course = courses.select("a");
            Elements schools1 = course.select("div");


            for(int i = 0; i<schools1.size(); i++){
                if(i%3 == 0){
                    Message.list31.add(schools1.get(i).text());
                }
                else if(i%3 == 1){
                    Message.list32.add(schools1.get(i).text());
                }
                else Message.list33.add(schools1.get(i).text());
            }



            for (int i = 0; i < course.size(); i++) {
                list.add("http://prog-crs.ust.hk" + course.attr("href"));
            }
        }catch(Exception e){
        }
        return list;
    }
}